package com.tanhua.sso.service;


import com.tanhua.sso.config.AliyunSMSConfig;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsService {
@Autowired
private AliyunSMSConfig aliyunSMSConfig;

@Autowired
private RedisTemplate<String,String> redisTemplate;

    public static final String CHECK_CODE = "check_code_";

    public ErrorResult sendCheckCode(String phone) {
        String check_code = CHECK_CODE + phone;

        //先判断该手机号码发送的验证码是否还未失效
        if(redisTemplate.hasKey(check_code)){
            return ErrorResult.builder().errCode("0001").errMessage("上次验证码未失效").build();
        }

        String code = "123456";
        if(StringUtils.isEmpty(code)){
            return ErrorResult.builder().errCode("0002").errMessage("发送验证码失败").build();

        }

        //短信验证码发送成功,将验证码保存到redis中,有效期为5分钟
        redisTemplate.opsForValue().set(check_code,code,5, TimeUnit.MINUTES);
        return null;
    }
}
